package com.hmdp.config;

import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * Api日志切面
 *
 * @author lizhihui
 */
@Slf4j
@Aspect
@Component
public class ApiLogAspect {

    /**
     * 切点表达式 - https://www.zhihu.com/question/377070748
     */
    @Pointcut("execution(public * com.hmdp.controller.*.*(..))")
    private void pointcut() { }

    @Around("pointcut()")
    public Object apiLog(ProceedingJoinPoint joinPoint) throws Throwable {
        final String methodName = joinPoint.getSignature().getName();
        final String className = joinPoint.getTarget().getClass().getSimpleName();
        final Object[] args = joinPoint.getArgs();

        log.info("### Entering " + className + "." + methodName + " with arguments: " + JSONUtil.toJsonStr(args));
        final Object result = joinPoint.proceed();
        log.info("### Exiting " + className + "." + methodName + " with result: " + JSONUtil.toJsonStr(result));
        return result;
    }

}
